package com.shujia.flink.tf;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class Demo2JavaMap {
    public static void main(String[] args) throws Exception {
        //创建flink的环境，注意包名
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        DataStream<String> studentDS = env.readTextFile("data/students.txt");

        /*
         * Tuple2:java中使用元组
         */
        DataStream<Tuple2<String, Integer>> kvDS = studentDS.map(new MapFunction<String, Tuple2<String, Integer>>() {
            @Override
            public Tuple2<String, Integer> map(String value) throws Exception {
                String[] split = value.split(",");
                String clazz = split[4];

                return Tuple2.of(clazz, 1);
            }
        });

        kvDS.print();

        env.execute();
    }
}
